package 代码记录.剑指offer错题本;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zx
 * @create 2022-06-01 10:33
 */
public class 顺时针打印矩阵_29 {
    public int[] spiralOrder(int[][] matrix) {
        if(matrix.length == 0){
            return new int[0];
        }
        int row = matrix.length;
        int col = matrix[0].length;
        int total = row * col;
        int count = 0;
        int[] res = new int[total];
        int left = 0,right = col - 1,up = 0,down = row - 1;
        while(count < total){
            for(int i = left;i <= right && count < total;i++){
                res[count] = matrix[up][i];
                count++;
            }
            up++;
            for(int i = up;i <= down && count < total;i++){
                res[count] = matrix[i][right];
                count++;
            }
            right--;
            for(int i = right;i >= left && count < total;i--){
                res[count] = matrix[down][i];
                count++;
            }
            down--;
            for(int i = down;i >= up && count < total;i--){
                res[count] = matrix[i][left];
                count++;
            }
            left++;
        }
        return res;
    }
}
